package com.dd.controller;

import com.dd.dto.OrderItemDTO;
import com.dd.entity.PurchaseOrders;
import com.dd.entity.PurchaseOrdersVO;
import com.dd.entity.R;
import com.dd.service.IPurchaseOrdersService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * <p>
 * 采购订单表 记录采购订单信息 前端控制器
 * </p>
 *
 * @author dhd
 * @since 2024-10-21
 */
@RestController
@RequestMapping("/purchaseOrders")
public class PurchaseOrdersController {
    @Autowired
    IPurchaseOrdersService purchaseOrdersService;

    //生成订单
    @PostMapping
    public R createOrder(@RequestBody List<OrderItemDTO> orderItems){
        purchaseOrdersService.createOrder(orderItems);
        return R.success();
    }

    //添加订单
    @PostMapping("/insert")
    public R insert(@RequestBody PurchaseOrders purchaseOrders){
        purchaseOrdersService.insert(purchaseOrders);
        return R.success();
    }

    //查询所有订单
    @GetMapping("/selectList")
    public R<PageInfo<PurchaseOrdersVO>> selectAll(@RequestParam Integer pageNum) {
        return R.success(purchaseOrdersService.selectAll(pageNum));
    }


    // 查询采购订单详情
    @GetMapping("/orderId")
    public R<List<PurchaseOrdersVO>> queryPurchaseOrderDetails(String orderId) {
        List<PurchaseOrdersVO> purchaseOrders = purchaseOrdersService.queryPurchaseOrders(orderId);
        return R.success(purchaseOrders);
    }


    //子订单发货按钮功能
    @GetMapping("/updateStatus")
    public R updateStatus(@RequestParam("orderId") String orderId,
                          @RequestParam("supplierId") int supplierId) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return R.success(purchaseOrdersService.updateStatus(orderId, supplierId));
    }

    //订单收货按钮功能
    @PostMapping("/updateStatusByOrderId")
    public R updateStatusByOrderId(@RequestParam(value = "orderId") String orderId) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
        return R.success(purchaseOrdersService.updateStatusByOrderId(orderId));
    }
}
